﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IBupro.Controllers.Classes;
using iBuproModel.Repositories.Interfaces;
using IBupro.Models;

namespace IBupro.Controllers
{

    /// <summary>
    /// Handles tags business logic
    /// </summary>
    public class TagController : CustomController
    {
        ITagRepository repositoryTag;

        public TagController()
        {
            this.repositoryTag = repositoryService.TagRepositoryCreateInstance();
        }

        public ActionResult List(string term) 
        {
            term = GetLastTag(term);
            term = term.ToLower().Trim();
            

            if (term == null)
                return Json(null);
            var results = from t in repositoryTag.GetAll().ToList()   
                    where t.Name.ToLower().Trim().StartsWith(term)  
                    select new { label = t.Name, id = t.Id  };  
   
             return Json(results.ToArray(), JsonRequestBehavior.AllowGet);  
         }
 

        private string GetLastTag(string tags)
        {
            char[] separators = new char[] {',',';'};
            string[] field = tags.Split(separators, StringSplitOptions.RemoveEmptyEntries);
            if (field.Length != 0)
                return field[field.Length - 1];
            else return "";
        }

        public ActionResult TagCloud()
        {
            ITagRepository repository = repositoryService.TagRepositoryCreateInstance();

            IEnumerable<TagCloudModel> list = from tag in repository.GetAll().ToList()
                                              where tag.Projects.Count() > 0
                                              select new TagCloudModel
                                              {
                                                Id = tag.Id,
                                                Name = tag.Name,
                                                Count = tag.Projects.Count(),
                                                CssClass = GetCssClassFromCount(tag.Projects.Count)
                                              };

            return View(list.ToList());
        }

        private string GetCssClassFromCount(int count)
        {
            if (count == 1)
                return "tagcloud-0";
            if (count > 1 && count <= 5)
                return "tagcloud-1";
            if (count > 5 && count <= 10)
                return "tagcloud-2";
            if (count > 10 && count <= 15)
                return "tagcloud-3";
            
            
                return "tagcloud-4";

        }

        public ActionResult ListProjects(int id)
        {
            ITagRepository repository = repositoryService.TagRepositoryCreateInstance();
            IDiplomProjectTypeRepository projectType = repositoryService.DiplomProjectTypeRepositoryCreateInstance();

            SearchDiplomProject search = new SearchDiplomProject();
            search.SearchQuery = repository.First(t => t.Id == id).Name;
            search.projectType = 0;

            return RedirectToAction("Index", "Home", new { search=search });

        }


    }
}
